<template>
	<el-dialog :title="title" v-model="open" append-to-body :close-on-click-modal="false" width="600px">
		<el-form ref="formRef" :model="form" :rules="rules" label-width="auto">
			<el-row :gutter="10" class="mb20">
				<el-col>
					<el-form-item label="分类名称" prop="name">
						<el-input v-model="form.name" placeholder="请输入名称" />
					</el-form-item>
				</el-col>
			</el-row>
			<el-row :gutter="10" class="mb20">
				<el-col :span="12">
					<el-form-item label="分类等级">
						<el-radio-group v-model="form.level">
							<el-radio :label="1">一级</el-radio>
							<el-radio :label="2">二级</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col>
				<el-col :span="12">
					<el-form-item label="排序" prop="display_order">
						<el-input v-model="form.display_order" placeholder="请输入排序" @input="onVerifiyNumberIntegerDisplayOrder" />
					</el-form-item>
				</el-col>
				<!-- <el-col :span="12">
					<el-form-item label="状态">
						<el-radio-group v-model="form.status">
							<el-radio :label="1">正常</el-radio>
							<el-radio :label="0">禁用</el-radio>
						</el-radio-group>
					</el-form-item>
				</el-col> -->
			</el-row>
			<el-row :gutter="10" class="mb20">
				<el-col :span="12" v-if="form.level == 2">
					<el-form-item label="所属上级" prop="pid">
						<el-select v-model="form.pid" placeholder="请选择所属上级" class="w100">
							<el-option v-for="item in classOptions" :key="item.value" :label="item.name" :value="item.id" />
						</el-select>
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
		<template #footer>
			<div class="dialog-footer">
				<el-button type="primary" :loading="submitLoading" @click="onSubmitForm"> 确 定 </el-button>
				<el-button @click="onCancel">取 消</el-button>
			</div>
		</template>
	</el-dialog>
</template>

<script lang="ts">
import { nextTick, reactive, ref, toRefs } from 'vue';
import { goodsClassAdd, goodsClassSelectList } from '/@/api/good/index';
import { ElMessage } from 'element-plus';
import { verifiyNumberInteger } from '/@/utils/toolsValidate';
export default {
	name: 'classificationAddDialog',
	components: {},
	setup() {
		const state = reactive({
			title: '添加分类',
			open: false,
			form: {} as any,
			rules: {
				name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
				pid: [{ required: true, message: '所属上级不能为空', trigger: ['blur'] }],
				display_order: [{ required: true, message: '排序不能为空', trigger: ['blur'] }],
			}, // 表单校验
			submitLoading: false,
			classOptions: [] as any,
		});

		const formRef = ref(null);
		const imageUploadThumbRef = ref(null);
		let callback: Function = () => {};

		/********** 点击事件(on开头) **********/
		// 排序过滤
		const onVerifiyNumberIntegerDisplayOrder = (val: string) => {
			state.form.money = verifiyNumberInteger(val);
		};
		/** 提交按钮 */
		const onSubmitForm = () => {
			(formRef.value as any)?.validate((valid: boolean) => {
				if (valid) {
					state.submitLoading = true;
					console.log('state.form', state.form);
					_goodsClassAdd({ ...state.form, pid: state.form.pid ? state.form.pid : 0 });
				}
			});
		};
		// 取消按钮
		const onCancel = () => {
			state.submitLoading = false;
			state.open = false;
			reset();
		};

		/********** 方法事件 **********/
		//打开
		const openDialog = (config: any, fn: Function) => {
			console.log(config);
			callback = fn;
			state.open = true;
			reset();
			nextTick(() => {
				(imageUploadThumbRef.value as any)?.dataLoading();
			});
			_goodsClassSelectList({ pageSize: 100, pageNum: 1, goods_class_id: 0 });
		};
		// 表单重置
		const reset = () => {
			state.form = {
				name: '',
				status: 1,
				level: 1,
				pid: '',
				display_order: 0,
			};
			(formRef.value as any)?.resetFields();
			nextTick(() => {
				(imageUploadThumbRef.value as any)?.reset();
			});
		};
		const remoteMethodClass = () => {};

		/**************************网络请求*************************/
		//添加分类
		const _goodsClassAdd = (params: any) => {
			goodsClassAdd(params)
				.then((res: any) => {
					console.log(res);
					state.submitLoading = false;
					ElMessage.success('操作成功');
					onCancel();
					callback();
				})
				.catch((err) => {
					state.submitLoading = false;
					console.log(err);
				});
		};
		//查询上级分类
		const _goodsClassSelectList = (params: any) => {
			goodsClassSelectList(params)
				.then((res: any) => {
					console.log(res);
					state.classOptions = res.data.list;
				})
				.catch((err) => {
					console.log(err);
				});
		};
		return {
			formRef,
			imageUploadThumbRef,
			onVerifiyNumberIntegerDisplayOrder,
			onSubmitForm,
			openDialog,
			onCancel,
			...toRefs(state),
			remoteMethodClass,
		};
	},
};
</script>

<style lang="scss" scoped></style>
